package net.shopxx.action.shop;

import java.math.BigDecimal;

import javax.annotation.Resource;

import net.shopxx.bean.Pager;
import net.shopxx.entity.AirOrder;
import net.shopxx.entity.Order;
import net.shopxx.service.AirOrderService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 后台Action类 - 订单
 */

@ParentPackage("shop")
@Results({ 
	@Result(name = "processAction", location = "airOrder!process.action", params = {"airOrder.id", "${airOrder.id}"}, type = "redirect")
})
public class AirOrderAction extends BaseShopAction {

	private static final long serialVersionUID = -2080980180511054311L;

	private AirOrder airOrder;

	@Resource
	private AirOrderService airOrderService;
	

	// 列表
	@SuppressWarnings("unchecked")
	public String list() {
		
		if (pager == null) {
			pager = new Pager();
		}
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(AirOrder.class);
		detachedCriteria.add(Restrictions.eq("member", getLoginMember()));
		pager = airOrderService.findByPager(pager,detachedCriteria);
		return LIST;
	}
	
	public String view()
	{
		airOrder = airOrderService.load(id);
		return VIEW;
	}

	// 删除
	public String delete() {
		try {
			airOrderService.delete(ids);
		} catch (Exception e) {
			return ajaxJsonErrorMessage("删除失败，订单数据被引用！");
		}
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 编辑
	public String edit() {
		airOrder = airOrderService.load(airOrder.getId());
		if (airOrder.getPaymentStatus() != net.shopxx.entity.AirOrder.PaymentStatus.unpaid) {
			addActionError("此订单付款状态无法编辑！");
			return ERROR;
		}
		setResponseNoCache();
		return INPUT;
	}
	
	@InputConfig(resultName = "error")
	public String update() {
		AirOrder persistent = airOrderService.load(airOrder.getId());
		if (persistent.getPaymentStatus() != net.shopxx.entity.AirOrder.PaymentStatus.unpaid) {
			addActionError("此订单付款状态无法编辑！");
			return ERROR;
		}
		if (airOrder.getDeliveryFee().compareTo(new BigDecimal("0")) < 0) {
			addActionError("配送费用不允许小于0！");
			return ERROR;
		}
		if (airOrder.getPaymentFee().compareTo(new BigDecimal("0")) < 0) {
			addActionError("支付费用不允许小于0！");
			return ERROR;
		}
		BeanUtils.copyProperties(airOrder, persistent, new String[] {"id", "createDate", "modifyDate", "airOrderSn", "airOrderStatus", "paymentStatus", "shippingStatus", "paidAmount", "memo", "member", "airOrderItemSet", "airOrderLogSet", "paymentSet", "refundSet", "shippingSet", "reshipSet"});
		airOrderService.update(persistent);
		redirectionUrl = "air_airOrder!list.action";
		return SUCCESS;
	}
	
	// 处理
	public String process() {
		airOrder = airOrderService.load(airOrder.getId());
		setResponseNoCache();
		return "process";
	}
}